package com.share.tool.dsc.service.impl;

import com.share.tool.dsc.mapper.NewMapper;
import com.share.tool.dsc.mapper.OldMapper;
import com.share.tool.dsc.model.IdsCount;
import com.share.tool.dsc.model.MinMax;
import com.share.tool.dsc.service.CompareService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.util.HashMap;

@Slf4j
@Service
public class CompareServiceImpl implements CompareService {
    final int step = 1000;

    @Autowired
    private OldMapper oldMapper;

    @Autowired
    private NewMapper newMapper;

    @Override
    public String byId(String table, String columnId, String columnTime, String startTime, String endTime) {
        MinMax minMax = getNewDbMinMaxcolumnId(table, columnId, columnTime, startTime, endTime);
        if(minMax ==null && minMax.getMin()==null){
            return "-1";
        }
        long maxId = minMax.getMax();

        //计算角标
        long startId = minMax.getMin();
        long endId = (startId+step)>maxId ? (startId+step):maxId;
        while (startId<=endId){
            IdsCount idsCount = getNewDbIdsCount(table, columnId, startId, endId);

            startId = endId;
            endId =(startId+step)>maxId ? (startId+step):maxId;
        }


        return "ff";
    }

    private MinMax getNewDbMinMaxcolumnId(String table, String columnId, String columnTime, String startTime, String endTime) {
        HashMap<Object, Object> map = new HashMap<>();
        map.put("table", table);
        map.put("columnId", columnId);
        map.put("columnTime", columnTime);
        map.put("startTime", "'" + startTime + "'");
        map.put("endTime", "'" + endTime + "'");
        return newMapper.getMinMaxcolumnId(map);
    }

    private IdsCount getNewDbIdsCount(String table, String columnId, long startId, long endId) {
        HashMap<Object, Object> map = new HashMap<>();
        map.put("table", table);
        map.put("columnId", columnId);
        map.put("startId", startId);
        map.put("endId", endId);
        return newMapper.getNewDbIdsCount(map);
    }

    @PostConstruct
    public void main() {
        String table = "tu_role";
        String columnId = "id";
        String columnTime = "create_time";
        String startTime = "2022-08-23 16:40:22";
        String endTime = "2022-10-23 16:40:22";
        String ids = byId(table, columnId, columnTime, startTime, endTime);
    }
}
